package cc.comac.mom.webservice.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cc.comac.mom.datahandlers.entity.QualityPclrTestInvoiceDataHandler;
import cc.comac.mom.entity.QualityTestContentEntity;
import cc.comac.mom.webservice.TestDataManageWebService;
@Component
@WebService(serviceName = "TestDataManage", targetNamespace = "http://webservice.mom.comac.cc/", endpointInterface = "cc.comac.mom.webservice.TestDataManageWebService")
public class TestDataManageWebServiceImpl implements TestDataManageWebService{

	@Autowired
	private QualityPclrTestInvoiceDataHandler qualityPclrTestInvoiceDataHandler;
	@Override
	public String getTestDataManage(Map<String, Object> map) {
		Map<String,String> resultMap = new HashMap<String,String>();
		resultMap.put("result", "true");
		resultMap.put("message", "");
		if (map.get("testNo")== null) {
			resultMap.put("result", "false");
			resultMap.put("message", "实验编号不能为空！");
		}
		
		if (map.get("testResult")== null) {
			resultMap.put("result", "false");
			resultMap.put("message", "实验结果不能为空！");
		}
		if (map.get("approved")== null) {
			resultMap.put("result", "false");
			resultMap.put("message", "批准不能为空！");
		}
		String testNo = map.get("testNo").toString();
		String testResult = map.get("testResult").toString();
		String approved = map.get("approved").toString();
		
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> list = (List<Map<String,Object>>)map.get("list");
		List<QualityTestContentEntity> TestContentEntityList = new ArrayList<QualityTestContentEntity>();
		for (Map<String, Object> map2 : list) {
			QualityTestContentEntity qualityTestContentEntity = new QualityTestContentEntity();
			qualityTestContentEntity.setTestName(map2.get("testName").toString());
			qualityTestContentEntity.setAttachmentName(map2.get("attachmentName").toString());
			qualityTestContentEntity.setIsConform(Integer.parseInt(map2.get("isConform").toString()));
			qualityTestContentEntity.setTestUser(map2.get("testUser").toString());
			qualityTestContentEntity.setOperation(map.get("operation") == null ? "":map.get("operation").toString());
			TestContentEntityList.add(qualityTestContentEntity);
		}
		// 更新实验内容数据和实验结果
		qualityPclrTestInvoiceDataHandler.updateByTestDateManage(testNo, testResult, approved, TestContentEntityList);
		return map.toString();
	}

	@Override
	public String getReciveData(Map<String, Object> map) {
		Map<String,String> resultMap = new HashMap<String,String>();
		resultMap.put("result", "true");
		resultMap.put("message", "");
		if (map.get("testNo") == null) {
			resultMap.put("result", "false");
			resultMap.put("message", "试验编号为空！");
			return resultMap.toString();
		}
		if (map.get("receiveUser") == null) {
			resultMap.put("result", "false");
			resultMap.put("message", "收料人为空！");
			return resultMap.toString();
		}
		if (map.get("receiveDate") == null) {
			resultMap.put("result", "false");
			resultMap.put("message", "收料时间为空！");
			return resultMap.toString();
		}
		if (map.get("deliverUser") == null) {
			resultMap.put("result", "false");
			resultMap.put("message", "送料人为空！");
			return resultMap.toString();
		}
		if (map.get("deliverDate") == null) {
			resultMap.put("result", "false");
			resultMap.put("message", "送料时间为空！");
			return resultMap.toString();
		}
		String testNo = map.get("testNo").toString();
		String receiveUser = map.get("receiveUser").toString();
		String receiveDate = map.get("receiveDate").toString();
		String deliverUser = map.get("deliverUser").toString();
		String deliverDate = map.get("deliverDate").toString();
		// 更新接收数据
		qualityPclrTestInvoiceDataHandler.updateReceiveDataByTestNo(testNo, receiveUser, receiveDate, deliverUser, deliverDate);
		return map.toString();
	}

}
